class GameLogic {
	public constructor() {
	}
	private static instance: GameLogic;
	public static getInstance(): GameLogic {
		if (this.instance == null) {
			this.instance = new GameLogic();
		}
		return this.instance;
	}

	private ui: GameUI;
	/** bgId,skinId,num */
	private args: any;

	public gamestate: number;

	private game_countId: number;
	private is_begin:boolean = false;
	private start_pos:egret.Point;
	private move_pos:egret.Point;
	private end_pos:egret.Point;
	private cur_pos:egret.Point;
	private mc:egret.MovieClip;

	private mc_player:Player;
	private _map:GameMap;
	public openUI(con: any, _ui: GameUI, arg: any = null): void {
		if (this.ui == null) {
			this.ui = _ui;
			this.args = arg;
			con.addChild(this.ui);
			this.ui.logic = this;
			if (this.ui.inited) {
				this.init();
			}
		}
	}

	public init() {
		console.log("GameLogic.init", this.ui.inited, this.ui.logic);
		this.checkFit();
		this.initData();
		this.initView();
		this.initBody();
		this.initEvent();

		
	}

	

	private checkFit() {
		var map_scale:number = GameData.stageHeight/this.ui.map_bg.height;

		this.ui.map_bg.height = GameData.stageHeight;
		// this.ui.map_bg.width = this.ui.map_bg.width * map_scale;
		this.ui.group_front.width = GameData.stageWidth;
		this.ui.group_front.height = GameData.stageHeight;
		// this. GameData.stageHeight
	}

	private initData() {
		this.start_pos = new egret.Point(0,0);
		this.move_pos = new egret.Point(0,0);
		this.end_pos = new egret.Point(0,0);
		this.end_pos.x = this.ui.map_bg.x;
		// this.ui.group_front.addEventListener(egret.TouchEvent.TOUCH_TAP, this.gront_click, this);
		this.ui.map_bg.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.trag_begin, this);
		this.ui.map_bg.addEventListener(egret.TouchEvent.TOUCH_END, this.trag_end, this);
		this.ui.map_bg.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.trag_move, this);
		this.ui.map_bg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.game_click, this);
		this.ui.group_front.touchEnabled = false;
		this.ui.group_back.touchEnabled = false;
		this.ui.group_far.touchEnabled = false;
		
		GameData.GameStage.addEventListener(egret.Event.ENTER_FRAME, this.enterframe, this);

		// const mcFactory:egret.MovieClipDataFactory = this.configItem.mcFactory
		// var data = RES.getRes('man2006_json');
		// var txtr = RES.getRes('man2006_png');
		// var mcFactory: egret.MovieClipDataFactory = new egret.MovieClipDataFactory( data, txtr );
		// // console.log(this.configItem.index)
		// this.mc = new egret.MovieClip( mcFactory.generateMovieClipData('man2006'));
		// this.ui.group_back.addChild(this.mc);
		// this.mc.gotoAndPlay('wait',-1);
		// this.mc.x = 300;
		// this.mc.y = 650;
		// this.mc.scaleX = -1;

		// this.mc_player = ObjectPool.getObject("Player") as Player;
		// this.mc_player.updateVO(new PlayerVO());
		// this.ui.group_back.addChild(this.mc_player);
		// this.mc_player.x = 600;
		// this.mc_player.y = 650;
		// this.mc_player.scaleX = -0.6;
		// this.mc_player.scaleY = 0.6;
		// this.mc_player.visible =false;

		this.ui.btn_left.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.left_click, this);
        this.ui.btn_right.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.right_click, this);
        this.ui.btn_up.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.up_click, this);

		

		var downDisplay:egret.DisplayObject = this.createBitmapByName('zfx_png');
		var sp:egret.Sprite = new egret.Sprite();
		sp.graphics.beginFill(0xFF0000,1);
		sp.graphics.drawRect(0,0,100,100);
		sp.graphics.endFill();
		// sp.addChild(downDisplay);
		sp.x = sp.y = 10;
		sp.width = sp.height = 100;
		this.ui.group_console.addChild(sp);
	}

	private initView() {
		
	}

	/**
	 * 创建刚体
	 */
	private initBody(){

		this._map = new GameMap();
        this._map.x = this._map.y = 0;
        this.ui.layer_floor.addChild(this._map);
        this._map.init(1);
        this.createMan(60,60,100,100,'man_png',this._map.p2World);
		
		var body_arr:Array<eui.Rect> = this.ui.getBodyDisPlay();
		console.log(body_arr);
		for(var i:number = 0;i<body_arr.length;i++){
			this._map.AddBody(body_arr[i]);
		}
	}

	private startGame() {
		
	}

	private c_index:number = 0;
	public game_click(e:egret.TouchEvent){
		this.c_index+=1;
		if(this.c_index>3){
			this.c_index = 0;
		}
		var arr = ['wait','walk','attack','resist'];

	}

	public trag_begin(e:egret.TouchEvent){
		this.is_begin = true;
		this.start_pos.x = e.stageX;
		this.start_pos.y = e.stageY;

		this.move_pos.x = e.stageX;
		this.move_pos.y = e.stageY;
		this.end_pos.x = this.ui.map_bg.x;
		console.log('gront_click=1');
	}

	public trag_end(e:egret.TouchEvent){
		this.is_begin = false;
		console.log('gront_click=2');
	}

	public trag_move(e:egret.TouchEvent){
		this.move_pos.x = e.stageX;
		this.move_pos.y = e.stageY;

		
	}


	private lefttime: number;
	private countDown() {
		
	}

	public gameover() {
		
	}

	private initEvent() {
		
	}

	 private left_click(e:TouchEvent){
        this.manBody.velocity[0] = -3;
        console.log('left');
    }

    private right_click(e:TouchEvent){
        this.manBody.velocity[0] = 3;
        console.log('right');
    }

    private up_click(e:TouchEvent){
        this.manBody.velocity[1] = 10;
        console.log('up');
    }

	private enterframe(e:egret.Event) {
		
		if(this.is_begin){
			var cur_x:number = this.move_pos.x;
			
			// console.log('e.localX='+cur_x+'-- this.start_pos.x = '+this.start_pos.x);
			// var to_x = this.ui.map_bg.x + (this.start_pos.x - cur_x)*0.7;
			var to_x = this.ui.map_bg.x - (this.start_pos.x-cur_x);
			to_x = to_x<GameData.stageWidth-this.ui.map_bg.width?GameData.stageWidth-this.ui.map_bg.width:to_x;
			to_x = to_x > 0?0:to_x;


			if(to_x<GameData.stageWidth-this.ui.map_bg.width){
				to_x = GameData.stageWidth-this.ui.map_bg.width;
			}

			if(to_x > 0){
				to_x = 0;
			}

			this.end_pos.x = to_x;
			this.start_pos.x = cur_x;
		}

		if(this.ui.map_bg.x != this.end_pos.x){
			console.log('this.end_pos='+this.end_pos.x+'xx='+this.ui.map_bg.x);
			//s = v*t;
			this.ui.map_bg.x += (this.end_pos.x-this.ui.map_bg.x)*0.7;
			this.ui.group_far.x += (this.end_pos.x-this.ui.map_bg.x)*0.2;
			// this._map._body_arr[0].position[0] = (this.ui.map_bg.x + 800*0.5)*0.02;	
			p2.vec2.set([],(this.ui.map_bg.x + 800*0.5)*0.02,0)
			
			if(Math.abs(Math.abs(this.ui.map_bg.x) - Math.abs(this.end_pos.x))<=1){
				this.ui.map_bg.x = this.end_pos.x;
				// this._map._body_arr[0].position[0] = (this.end_pos.x + 800*0.5)*0.02;	
				p2.vec2.set([],(this.end_pos.x + 800*0.5)*0.02,0)
			}

			if(Math.abs(Math.abs(this.ui.group_far.x) - Math.abs(this.end_pos.x))<=1){
				this.ui.group_far.x = this.end_pos.x;
			}
		}

		if(this.c_index == 1){

		}
		
		
	}

	private clearEvent() {		
		GameData.GameStage.removeEventListener(egret.Event.ENTER_FRAME, this.enterframe, this);
	}



	public clear() {
		this.clearEvent();
		if(this.ui != null && this.ui.parent != null){
			this.ui.parent.removeChild(this.ui);
		}
		this.ui = null;
	}

	private manBody:p2.Body;
    private createMan(w:number,h:number,x:number,y:number,resid:string,world: p2.World){
        var factorc = 0.02;
        var factorx = 50;
        var stageHeight:number = GameData.stageHeight;//egret.MainContext.instance.stage.stageHeight;

        var boxShape: p2.Shape = new p2.Box({width: w*factorc, height: h*factorc});
        var boxBody: p2.Body = new p2.Body({ mass: 30, position: [(x+w*0.5)*factorc, (stageHeight - (y+h*0.5))*factorc]});
        boxBody.addShape(boxShape);
        world.addBody(boxBody);
        var downDisplay:egret.DisplayObject = this.createBitmapByName(resid);
        boxBody.fixedRotation = true;

        downDisplay.width = w;
        downDisplay.height = h;

        downDisplay.x = boxBody.position[0] * factorx;
        downDisplay.y = stageHeight - boxBody.position[1] * factorx;

        downDisplay.anchorOffsetX = downDisplay.width / 2;
        downDisplay.anchorOffsetY = downDisplay.height / 2;
        console.log(downDisplay.x,downDisplay.y);
        this.ui.addChild(downDisplay);
        boxBody.displays = [downDisplay];

        this.manBody = boxBody;
    }

	private createBitmapByName(name: string): egret.Bitmap {
        var result: egret.Bitmap = new egret.Bitmap();
        var texture: egret.Texture = RES.getRes(name);
        result.texture = texture;
        return result;
    }
}